﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WeChatParking.Core.BLL
{
    public class Wx_user
    {
        public Entity.Wx_user GetWxUserByOpenId(string openid)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var p = new { openid = openid };
                return dbContext.GetList<Entity.Wx_user>(p).FirstOrDefault();
            }
        }

        public Entity.Wx_user GetWxUserById(int id)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var p = new { id = id };
                return dbContext.GetList<Entity.Wx_user>(p).FirstOrDefault();
            }
        }
        public Entity.Wx_user GetWxUserByNickname(string Nickname)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var p = new { nickname = Nickname };
                return dbContext.GetList<Entity.Wx_user>(p).FirstOrDefault();
            }
        }
        /// <summary>
        /// 添加微信用户
        /// </summary>
        /// <param name="wxUser">微信用户</param>
        /// <returns></returns>
        public int AddWxUser(Payment.WeixinPay.WxUserInfo wxUser)
        {
            Entity.Wx_user user = new Entity.Wx_user()
            {
                openid = wxUser.openid,
                nickname = wxUser.nickname,
                sex = wxUser.sex,
                city = wxUser.city,
                province = wxUser.province,
                country = wxUser.country,
                headimgurl = wxUser.headimgurl,
                create_user = "SYSTEM",
                create_time=DateTime.Now,
                update_user="SYSTEM",
                update_time=DateTime.Now,
                version=0
            };
            using (var dbContext = Repository.DbContext.Create())
            {
                return dbContext.Insert(user);
            }
        }

     
    }
}
